﻿using System.Collections.Generic;
using System.Drawing;
using Dataweb.NShape.Advanced;
using Dataweb.NShape;
using System.ComponentModel;
using cz.zcu.PreSti.Components;
using System;

namespace cz.zcu.PreSti.Shapes
{
	public class ImageStimulusSymbol : StimulusSymbol
	{
		
		protected override void InitializeToDefault(IStyleSet styleSet)
		{
			base.InitializeToDefault(styleSet);
			Text = "Image";
		}

		public override void CopyFrom(Shape source)
		{
			base.CopyFrom(source);
		}

		public override int SettersCount
		{
			get { return 9; }
		}

		public override string GetTitleForSetter(int id)
		{
			string tmp = base.GetTitleForSetter(id);
			switch (id)
			{
				case 0: tmp = "Delay"; break;
				case 1: tmp = "Path"; break;
				case 2: tmp = "X Position"; break;
				case 3: tmp = "Y Position"; break;
				case 4: tmp = "Width"; break;
				case 5: tmp = "Height"; break;
				case 6: tmp = "Rotation center X"; break;
				case 7: tmp = "Rotation center Y"; break;
				case 8: tmp = "Rotation angle"; break;
			}
			return tmp;
		}

		public override Shape Clone()
		{
			Shape result = new ImageStimulusSymbol(Type, (Template)null);
			result.CopyFrom(this);
			return result;
		}

		protected internal ImageStimulusSymbol(ShapeType shapeType, Template template)
			: base(shapeType, template)
		{
		}

		protected internal ImageStimulusSymbol(ShapeType shapeType, IStyleSet styleSet)
			: base(shapeType, styleSet) {
		}

		#region ISymbol Members

		public override BaseComponent initializeComponent(IPresentation p)
		{
			ImageStimulusModel model = (ImageStimulusModel)this.ModelObject;
			ImageStimulus stimulus = new ImageStimulus(p);
			stimulus.Delay = model.Duration;
			stimulus.Path = model.ImagePath;
			stimulus.Position = new Point(model.X, model.Y);
			stimulus.Size = new SizeF(model.SizeWidth, model.SizeHeight);
			stimulus.RotationCenter = new PointF(model.RotationCenterX, model.RotationCenterY);
			stimulus.Rotation = model.Rotation;
			stimulus.loadTexture();
			return stimulus;
		}

		#endregion

		internal static Shape CreateInstance(ShapeType shapeType, Template template)
		{
			return new ImageStimulusSymbol(shapeType, template);
		}

	}

	public class ImageStimulusModel : ModelObjectBase
	{
		#region Fields

		private int duration = 5000;
		private string imagePath;
		private Point position = Point.Empty;
		private SizeF size = SizeF.Empty;
		private PointF rotationCenter = PointF.Empty;
		private float rotation = 0;
		private char securityDomainName = 'A';

		private const int PropertyIdDuration = 1;
		private const int PropertyIdImagePath = 2;
		private const int PropertyIdX = 3;
		private const int PropertyIdY = 4;
		private const int PropertyIdSizeWidht = 5;
		private const int PropertyIdSizeHeight = 6;
		private const int PropertyIdRotationCenterX = 7;
		private const int PropertyIdRotationCenterY = 8;
		private const int PropertyIdRotation = 9;

		#endregion

		#region properties

		[Category("Presentation")]
		[Description("Duration of the stimulus in miliseconds.")]
		[PropertyMappingId(PropertyIdDuration)]
		public virtual int Duration
		{
			get
			{
				return duration;
			}
			set
			{
				duration = value;
			}
		}

		[Category("Presentation")]
		[Description("Text of the stimulus.")]
		[Editor(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
		[PropertyMappingId(PropertyIdImagePath)]
		public virtual string ImagePath
		{
			get
			{
				return imagePath;
			}
			set
			{
				imagePath = value;
			}
		}

		[Category("Presentation")]
		[Description("X-axis position of the image.")]
		[PropertyMappingId(PropertyIdX)]
		public virtual int X
		{
			get
			{
				return position.X;
			}
			set
			{
				position.X = value;
			}
		}

		[Category("Presentation")]
		[Description("Y-axis position of the image.")]
		[PropertyMappingId(PropertyIdY)]
		public virtual int Y
		{
			get
			{
				return position.Y;
			}
			set
			{
				position.Y = value;
			}
		}

		[Category("Presentation")]
		[Description("Width of the image.")]
		[PropertyMappingId(PropertyIdSizeWidht)]
		public virtual float SizeWidth
		{
			get
			{
				return size.Width;
			}
			set
			{
				size.Width = value;
			}
		}

		[Category("Presentation")]
		[Description("Height of the image.")]
		[PropertyMappingId(PropertyIdSizeHeight)]
		public virtual float SizeHeight
		{
			get
			{
				return size.Height;
			}
			set
			{
				size.Height = value;
			}
		}

		[Category("Presentation")]
		[Description("X-axis position of the rotation center.")]
		[PropertyMappingId(PropertyIdRotationCenterX)]
		public virtual float RotationCenterX
		{
			get
			{
				return rotationCenter.X;
			}
			set
			{
				rotationCenter.X = value;
			}
		}

		[Category("Presentation")]
		[Description("Y-axis position of the rotation center.")]
		[PropertyMappingId(PropertyIdRotationCenterY)]
		public virtual float RotationCenterY
		{
			get
			{
				return rotationCenter.Y;
			}
			set
			{
				rotationCenter.Y = value;
			}
		}

		[Category("Presentation")]
		[Description("Rotation of the image measured in radians.")]
		[PropertyMappingId(PropertyIdRotation)]
		public virtual float Rotation
		{
			get
			{
				return rotation;
			}
			set
			{
				rotation = value;
			}
		}

		#endregion

		public static ImageStimulusModel CreateInstance(ModelObjectType modelObjectType) {
			return new ImageStimulusModel(modelObjectType);
		}


		protected internal ImageStimulusModel(ModelObjectType modelObjectType)
			: base(modelObjectType) {
				this.terminalCount = 12;
		}


		protected internal ImageStimulusModel(ImageStimulusModel source)
			: base(source) {
			Duration = source.Duration;
			ImagePath = source.ImagePath;
			X = source.X;
			Y = source.Y;
			SizeWidth = source.SizeWidth;
			SizeWidth = source.SizeHeight;
			RotationCenterX = source.RotationCenterX;
			RotationCenterY = source.RotationCenterY;
			Rotation = source.Rotation;
			this.terminalCount = 12;
		}


		public override IModelObject Clone() {
			return new ImageStimulusModel(this);
		}


		public override IEnumerable<MenuItemDef> GetMenuItemDefs() {
			throw new NotImplementedException();
		}


		public override void Connect(TerminalId ownTerminalId, IModelObject targetConnector, TerminalId targetTerminalId) {
		}


		public override void Disconnect(TerminalId ownTerminalId, IModelObject targetConnector, TerminalId targetTerminalId) {
		}

		public override char SecurityDomainName
		{
			get { return securityDomainName; }
			set
			{
				if (value < 'A' || value > 'Z')
					throw new ArgumentOutOfRangeException("SecurityDomainName", "The domain qualifier has to be an upper case  ANSI letter (A-Z).");
				securityDomainName = value;
			}
		}


		#region IEntity Members

		public static new IEnumerable<EntityPropertyDefinition> GetPropertyDefinitions(int version)
		{
			foreach (EntityPropertyDefinition pi in ModelObjectBase.GetPropertyDefinitions(version))
				yield return pi;
			yield return new EntityFieldDefinition("Duration", typeof(int));
			yield return new EntityFieldDefinition("ImagePath", typeof(string));
			yield return new EntityFieldDefinition("X", typeof(int));
			yield return new EntityFieldDefinition("Y", typeof(int));
			yield return new EntityFieldDefinition("SizeWidth", typeof(float));
			yield return new EntityFieldDefinition("SizeHeight", typeof(float));
			yield return new EntityFieldDefinition("RotationCenterX", typeof(float));
			yield return new EntityFieldDefinition("RotationCenterY", typeof(float));
			yield return new EntityFieldDefinition("Rotation", typeof(float));
		}

		protected override void LoadFieldsCore(IRepositoryReader reader, int version)
		{
			base.LoadFieldsCore(reader, version);
			Duration = reader.ReadInt32();
			ImagePath = reader.ReadString();
			X = reader.ReadInt32();
			Y = reader.ReadInt32();
			SizeWidth = reader.ReadFloat();
			SizeHeight = reader.ReadFloat();
			RotationCenterX = reader.ReadFloat();
			RotationCenterY = reader.ReadFloat();
			Rotation = reader.ReadFloat();
		}

		protected override void SaveFieldsCore(IRepositoryWriter writer, int version)
		{
			base.SaveFieldsCore(writer, version);
			writer.WriteInt32(Duration);
			writer.WriteString(ImagePath);
			writer.WriteInt32(X);
			writer.WriteInt32(Y);
			writer.WriteFloat(SizeWidth);
			writer.WriteFloat(SizeHeight);
			writer.WriteFloat(RotationCenterX);
			writer.WriteFloat(RotationCenterY);
			writer.WriteFloat(Rotation);
		}

		#endregion


	}

}
